/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

/**
 *
 * @author Hugo
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.Aluno;
import dao.ConnectionFactory;
import java.sql.Statement;
import model.Endereco;

/**
 *
 * @author Hugo
 */
public class EnderecoDAO {

    private Connection connection;

    public EnderecoDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    /**
     *
     * @param endereco
     */
    public int incluir(Endereco endereco) {
        String sql = "insert into endereco (logradouro, numero, complemento, bairro, cep, cidade, estado) values (?,?,?,?,?,?,?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, endereco.getLogradouro());
            stmt.setInt(2, endereco.getNumero());
            stmt.setString(3, endereco.getComplemento());
            stmt.setString(4, endereco.getBairro());
            stmt.setString(5, endereco.getCep());
            stmt.setString(6, endereco.getCidade());
            stmt.setString(7, endereco.getEstado());
            stmt.execute();
            ResultSet rs = stmt.getGeneratedKeys();
            int id = -1;
            if (rs.next()) {
                id = rs.getInt(1);
            }
            stmt.close();
            return id;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int alterar(Endereco endereco) throws SQLException {
        String sql = "update endereco set logradouro = ?, numero = ?, complemento = ?, bairro = ?, cep = ?, cidade = ?, estado = ? where id = ?";
        int id = -1;

        PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, endereco.getLogradouro());
        stmt.setInt(2, endereco.getNumero());
        stmt.setString(3, endereco.getComplemento());
        stmt.setString(4, endereco.getBairro());
        stmt.setString(5, endereco.getCep());
        stmt.setString(6, endereco.getCidade());
        stmt.setString(7, endereco.getEstado());
        stmt.setInt(8, endereco.getId());
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            id = rs.getInt(1);
        }
        stmt.close();

        return id;
    }

    public void excluir(Endereco endereco) {
        try {
            String sql = "delete from endereco where id = ?";
            PreparedStatement stmt1 = connection.prepareStatement(sql);
            stmt1.setInt(1, endereco.getId());
            stmt1.execute();
            stmt1.close();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Não é possível excluir");
        }
    }
}
